package com.beemans.corekit.integration.polling;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import java.util.concurrent.atomic.AtomicLong;

public class UserPresentWorker extends Worker {
    // 上一次检索时间戳
    private static final AtomicLong lastCheckInterval = new AtomicLong(0);

    public UserPresentWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        // 系统为了减少电量消耗，可能会将触发时间临近的几个任务放在一起执行
        final long now = System.currentTimeMillis();
        final long last = lastCheckInterval.get();
        if (Math.abs(now - last) < 2000) {
            return Result.success();
        }

        lastCheckInterval.set(now);

        LocalPolling.getInstance().checkPolling(true);
        return Result.success();
    }
}